package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.UserAddNewDTO;
import cn.tedu.suppermemory.pojo.dto.UserLoginDTO;
import cn.tedu.suppermemory.pojo.vo.UserListItemVO;
import cn.tedu.suppermemory.pojo.vo.UserStandardVO;
import cn.tedu.suppermemory.security.LoginPrincipal;
import cn.tedu.suppermemory.service.IUserService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 用户的控制类
 *
 * @author java@wen
 * @version 0.0.1
 */
@RestController
@Slf4j
@RequestMapping("/users")
@Api(tags = "01. 用户管理模块")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据id删除删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询用户详情")
    @ApiOperationSupport(order = 420)
    @GetMapping("/id:[0-9]+}")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    public JsonResult<UserStandardVO> list(Long id) {
        log.debug("开始处理【查询用户详情】的请求，参数：无");
        UserStandardVO standardById = userService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list() {
//    public JsonResult<List<UserListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询用户列表】的请求，参数：无");
//        log.debug("当事人：{}", loginPrincipal);
//        log.debug("当事人的ID：{}", loginPrincipal.getId());
//        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

}
